import { JSONMapping } from "../../json-mapping";
import { T } from "@angular/core/src/render3";
import {SyncStatus} from "../../sync-status";
import {ObjectDtoInterface} from "../../../services/sqliteDB/object.dto";
import { Attachment } from "../attachment";

export class InspectionRecords extends JSONMapping implements  ObjectDtoInterface {
    static PropertyKeyToJSONKeyMap() {
        return {
            taskId: "planId",
            author: "planCode",
            noteTaker: "recorder",
            inspectedDate: "explorationTime",
            company: "explorationDept",
            stackHolder: "explorationLeader",
            members: "explorationMan",
            taskContent: "jobTask",
            workingArea: "items1",
            powerCutScope: "items2",
            powerCutDevices: "items3",
            stateGrounding: "items4",
            workGrounding: "items5",
            keepLivePowerDevicePositions: "items6",
            badges: "items7",
            planedStartedTime: "items8StartTime",
            planedEndTime:  "items8EndTime",
            stopPowerStartTime: "items9PowerCutTime",
            stopPowerEndTime: "items9PowerCutTimeSlot",
            supplyPowerStartTime: "items9PowerOnTime",
            supplyPowerEndTime: "items9PowerOnTimeSlot",
            isChangingPowerLine: "items10IsPowerOn",
            changingPowerLineInfo: "items10Remark",
            isAdjacentLines: "items11IsElectric",
            adjacentLinesRemark: "items11Remark",
            crossLineInfo: "items12",
            hasDoublePowers: "items13",
            devicesUnderGround: "items14",
            risks: "items15",
            riskLevel: "items16",
            powerCutDiagram: "items18",
            pictureAttachmentIds: "items17",
            saveLocaTime: "saveToLocalTime",
            submitServeTime: "createdTime",
            recordsId: "recordsId|id"
        };
    }
    public recordsId: number = null;
    public taskId: string = null; // 检修任务Id
    public author: string = null;  // 检修任务编号
    public noteTaker: string = null;  // 记录人
    public noteTakerId: string = null;  // 记录人
    public inspectedDate: number  = null;      // 勘察日期
    public company: string = null;     // 勘察单位名字
    public stackHolder: string = null;       // 勘察负责人名字
    public stackHolderId: string = null;      // 勘察负责人Id
    public memberIds: string  = null;      // 勘察人员的Id列表 ，逗号分隔
    public members: string = null;        // 勘察人员的名字，逗号分隔
    public taskContent: string = null;     // 勘察任务内容
    public workingArea: string = null;      // 工作区域
    public workingAreaDeviceId: string = null;       // 工作区域关联的设备Id
    public powerCutScope: string = null;       // 停电区域
    public powerCutScopeDeviceId: string = null;     // 停电区域关联的设备Id
    public powerCutDevices: string = null;      // 停电设备
    public powerCutDeviceIds: string = null;       // 停电设备Id列表，逗号分隔
    public stateGrounding: string = null;       // 应挂状态接地
    public workGrounding: string = null;       // 应挂工作接地
    public keepLivePowerDevicePositions: string = null;       // 保留带电部位
    public badges: string = null;       // 应悬挂标识牌
    public planedStartedTime: number = null;       // 计划开始时间
    public planedEndTime: number = null;       // 计划结束时间
    public powerCutHours: number = null;       // 计划停电小时数
    public stopPowerStartTime: number = null; // 计划停电开始时间
    public stopPowerEndTime: number = null; // 计划停电结束时间
    public supplyPowerStartTime: number = null; // 计划送电开始时间
    public supplyPowerEndTime: number = null; // 计划送电结束时间
    public isChangingPowerLine: boolean = null;       // 是否转供电
    public changingPowerLineInfo: string  = null;      // 转供电相关信息
    public isAdjacentLines: boolean = null;       // 邻近线路是否供电
    public adjacentLinesRemark: string = null;       // 邻近线路备注
    public crossLineInfo: string  = null;      // 停电区域交叉跨越情况
    public hasDoublePowers: boolean  = null;      // 停电区域有无双电源，自备电源用户
    public devicesUnderGround: boolean   = null;     // 工作区域的地下管线设施
    public risks: string  = null;     // 作业危险点
    public riskLevel: string = null;      // 作业风险等级
    public powerCutDiagram: string   = null;   // 停电示意图
    public saveLocaTime: number = null;   // 保存到本地时间
    public submitServeTime: number = null; // 提交到服务器时间
    public pictureAttachmentIds: string   = null;     // 现成图片附件Id列表，逗号分隔
    public pictureAttachments: Array<Attachment> = [];
    public syncStatus: SyncStatus = SyncStatus.None;       // 同步状态：   （0: 从来没上传, 1: 上传成功, 2: 正在上传, 3: 上传失败)


    constructor(json: { [key: string]: any }) {
        super(json);
        InspectionRecords.mapFromJSON(json, this);
    }

    addAttachments(paths: Array<string>): Array<Attachment> {
        let attachments = paths.map((path, index) => {
          let id = new Date().getTime() + '' + (Math.ceil(Math.random() * 1000)) + '' + index;
          let name = path.split('/');
          return new Attachment({id: id , attachmentName: name[name.length - 1], path: path, syncStatus: SyncStatus.None});
        });
        return attachments;
      }

    updateAttachmentIdWithNewId(oldAttachmentId: string, newAttachmentId: string) {
      let attachment: Attachment = null;
      if (this.pictureAttachments.some((pictureAttachment, index) => {
        let result = pictureAttachment.id === oldAttachmentId;
        if (result) {
          attachment = pictureAttachment;
        }
        return result;
      })) {
        attachment.id = newAttachmentId;

      }
    }
}
